package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.bo.CommonListBo;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.service.AdminCommentService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminCommentServiceImpl implements AdminCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Override
    public List<MarketComment> list(CommonListBo commonListBo) {
        String page = commonListBo.getPage().toString();
        String limit = commonListBo.getLimit().toString();
        String sort = commonListBo.getSort();
        String order = commonListBo.getOrder();
        String userId = commonListBo.getUserId().toString();
        String valueId = commonListBo.getValueId().toString();
        MarketCommentExample marketCommentExample = new MarketCommentExample();

        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        if(!StringUtils.isEmpty(page) && !StringUtils.isEmpty(limit)){
            PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        }else {
            PageHelper.startPage(0,Integer.parseInt(limit));

        }
        marketCommentExample.setOrderByClause(sort + " " + order);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);
        return marketComments;
    }

    @Override
    public void delete(String id) {
        MarketCommentExample marketGoodsExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(Integer.parseInt(id));
        marketCommentMapper.deleteByExample(marketGoodsExample);
    }
}
